// index.js
const {addressList} = require('../../apiConfig/apiList/luban');
const {repairList} = require('../../utils/repairData');
const {cateList} = require('../../utils/cateData');
const {moviesList} = require('../../utils/moviesData');
Page({
  data: {
    active: 0,
    latitude: '',
    longitude: '',
    schoolName: wx.getStorageSync('loginData').schoolName,
    acticleList: [],
    formData: {},
    cardList: repairList,
    cateList: cateList,
    movies: moviesList,
    // [
    //   {
    //     name: '教学区',
    //     value: 1,
    //     img: 1,
    //     children: [
    //       {name: '课桌椅', value: '23'},
    //       {name: '门窗', value: '24'},
    //       {name: '灯具', value: '25'},
    //       {name: '给排水', value: '26'},
    //       {name: '灯具', value: '23'},
    //       {name: '桌椅', value: '24'},
    //     ],
    //   },
    //   {
    //     name: '生活区',
    //     value: 1,
    //     children: [
    //       {name: '灯具', value: '23'},
    //       {name: '给排水', value: '24'},
    //     ],
    //   },
    //   {
    //     name: '书橱',
    //     value: 1,
    //     children: [
    //       {name: '灯具', value: '23'},
    //       {name: '桌椅', value: '24'},
    //     ],
    //   },
    //   {
    //     name: '办公区',
    //     value: 1,
    //     children: [
    //       {name: '水龙头', value: '23'},
    //       {name: '供暖系统', value: '24'},
    //     ],
    //   },
    // ],
    showPanel: false,
    actions: [
      {
        name: '灯具',
        value: '1',
      },
      {
        name: '书橱',
        value: '2',
      },
      {
        name: '水龙头',
        value: '3',
      },
      {
        name: '供暖系统',
        value: '4',
      },
    ],
    showTree: false,
    columns: [
      {
        values: repairList,
        className: 'column1',
      },
      {
        values: repairList[0].children,
        className: 'column2',
        defaultIndex: 2,
      },
    ],
  },
  testFunc(){
    // let data = wx.getStorageSync('loginData')
    // data.authToken = ''
    // wx.setStorageSync('loginData', data)
    // this.getAddress();
  },
  showMenuList: function(){
    this.setData({ showTree: true });
    console.log(423423423, this.data.showTree)
  },
  selectRepair(event) {
    const { value } = event.detail;
    let list = [];
    value.forEach(item=>{
      list.push(item.text)
    })
    this.setData({
      'formData.serviceName': list.join(',')
    })
    this.setData({ showTree: false });
  },
  cancelSelect() {
    this.setData({ showTree: false });
  },
  onChange(event) {
    const { value } = event.detail;
    let id = value[0].id;
    let temp = [
      {
        values: repairList,
        className: 'column1',
      },
      {
        values: repairList[id - 1].children,
        className: 'column2',
      },
    ]
    this.setData({
      columns: temp
    })
  },
  onShow(){
    this.getAddress();
  },
  showCate: function(){
    this.setData({ showPanel: true });
  },
  onCancel() {
    this.setData({ showPanel: false });
  },
  doClose() {
    this.setData({ showPanel: false });
  },

  onSelectData(e) {
    console.log('onSelect', e.detail);
    let data = e.detail;
    this.setData({
      'formData.name': e.detail.name,
      'formData.name': e.detail.name,
      'formData.phone': e.detail.phone,
      'formData.id': e.detail.id,
      'formData.schoolId': e.detail.value,
    })
    console.log('onSelectData', this.data.formData)
    this.setData({ showPanel: false });
  },
  getAddress(){
    const that = this;
    let params = {
      pageNum: 1,
      pageSize: 100,
      orderByColumn: 'create_time',
      isAsc: 'desc',
    };
    addressList(params).then(res=>{
      if(res.code == 200){
        let data = res.rows;
        let temp = [];
        data.forEach(item=>{
          temp.push({
            name: item.schoolName + item.detailAddress,
            value: item.schoolId,
            id: item.id,
            phone: item.phone,
          })
        })
        this.setData({
          actions: temp
        });
      }
    })
  },
  toKnowledge(){
    wx.redirectTo({
      url: '/pages/maintenance-knowledge/index',
    })
  },
  toQuestion(){
    wx.redirectTo({
      url: '/pages/maintenance-questions/index',
    })
  },
  goNext(){
    let data = JSON.stringify(this.data.formData)
    wx.navigateTo({
      url: `/pages/pre-book/index?data=${data}`,
    })
  },
  selectCard(e){
    const address = this.data.actions?this.data.actions[0]:{}
    this.setData({
      'formData.serviceName': e.currentTarget.dataset.card,
      'formData.name': address.name||'',
      'formData.phone': address.phone||'',
      'formData.id': address.id||'',
      'formData.schoolId': address.value||'',
    })
    let data = JSON.stringify(this.data.formData)
    wx.navigateTo({
      url: `/pages/pre-book/index?data=${data}`,
    })
  },
})
